﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Axis.Web
{
    public static class IPHelper
    {
        [DllImport("iphlpapi.dll", ExactSpelling = true)]
        public static extern int SendARP(int DestIP, int SrcIP, byte[] pMacAddr, ref uint PhyAddrLen);

        public static string getMAC(IPAddress address)
        {
            int intAddress = BitConverter.ToInt32(address.GetAddressBytes(), 0);

            byte[] macAddr = new byte[6];
            uint macAddrLen = (uint)macAddr.Length;
            if (SendARP(intAddress, 0, macAddr, ref macAddrLen) != 0)
                return "(NO ARP result)";

            string[] str = new string[(int)macAddrLen];
            for (int i = 0; i < macAddrLen; i++)
                str[i] = macAddr[i].ToString("x2");

            return string.Join(":", str);
        }
    }
    public partial class Tests : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //string MachineName = Server.MachineName;
                string UserHostName = Request.UserHostName;
                //string UserHostAddress = Request.UserHostAddress;
                //string DnsHostName = System.Net.Dns.GetHostName();
                string REMOTE_ADDR = Request.ServerVariables["REMOTE_ADDR"].ToString();
                string REMOTE_HOST = Request.ServerVariables["REMOTE_HOST"].ToString();
                string REMOTE_USER = Request.ServerVariables["REMOTE_USER"].ToString();
                //string URL = Request.ServerVariables["URL"].ToString();
                //string SERVER_NAME = Request.ServerVariables["SERVER_NAME"].ToString();
                //string AUTH_USER= Request.ServerVariables["AUTH_USER"];
                IPHostEntry host = Dns.GetHostEntry(Request.ServerVariables["REMOTE_ADDR"].ToString());

                string compName = DetermineCompName(UserHostName);
                StringBuilder ret = new StringBuilder();
                //ret.AppendFormat("MachineName: {0}<br/>", MachineName);
                //ret.AppendFormat("UserHostName: {0}<br/>", UserHostName);
                //ret.AppendFormat("UserHostAddress: {0}<br/>", UserHostAddress);
                //ret.AppendFormat("DnsHostName: {0}<br/>", DnsHostName);

                ret.AppendFormat("REMOTE_ADDR: {0}<br/>", REMOTE_ADDR);
                ret.AppendFormat("REMOTE_HOST: {0}<br/>", REMOTE_HOST);
                ret.AppendFormat("REMOTE_USER: {0}<br/>", REMOTE_USER);
                //ret.AppendFormat("URL: {0}<br/>", URL);
                //ret.AppendFormat("SERVER_NAME: {0}<br/>", SERVER_NAME);
                //ret.AppendFormat("AUTH_USER: {0}<br/>", AUTH_USER);
                ret.AppendFormat("HostName: {0}<br/>", host.HostName);
                ret.AppendFormat("compName: {0}<br/>", compName);
                //NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
                //if (nics != null && nics.Length > 0) {
                //    foreach (NetworkInterface n in nics) {
                //        IPInterfaceProperties ipPro = n.GetIPProperties();
                        
                //        IPv4InterfaceStatistics ipv4Static = n.GetIPv4Statistics();
                        
                //        ret.AppendFormat("NetworkInterface--> Name: {0}<br/>", n.Name);
                //    }
                //}

                //ret.AppendFormat("GetComputerName: {0}<br/>", GetComputerName(UserHostAddress));
                //ret.Append(DoGetHostEntry(UserHostAddress));
                ltrMsg.Text = GetIP();
            }
        }
        public string GetIP()
        {
            
            IPHostEntry host = Dns.GetHostEntry(Request.ServerVariables["REMOTE_ADDR"].ToString());
            string clientMachineName = host.HostName;
            IPHostEntry ipEntry = Dns.GetHostEntry(clientMachineName);
            IPAddress[] addr = ipEntry.AddressList;
            string ret = "ClientMachineName: " + clientMachineName + "<br/>";
            int count = 0;
            if (addr != null && addr.Length > 0)
            {
                foreach (IPAddress ad in addr)
                {
                    ret += string.Format("[{0}]: {1}<br />", count, IPHelper.getMAC(ad));
                    count++;
                }
            }
            return ret;
        }
        //public string GetComputerName(string clientIP)
        //{
        //    try
        //    {
        //        var hostEntry = Dns.GetHostEntry(clientIP);
        //        return hostEntry.HostName;
        //    }
        //    catch (Exception ex)
        //    {
        //        return string.Empty;
        //    }
        //}
        //public string DoGetHostEntry(string hostname)
        //{
        //    StringBuilder ret = new StringBuilder();

        //    IPHostEntry host;
        //    host = Dns.GetHostEntry(hostname);
        //    Console.WriteLine("GetHostEntry({0}) returns:", hostname);
        //    foreach (IPAddress ip in host.AddressList)
        //    {
        //        ret.AppendFormat("DoGetHostEntry: {0}<br/>", ip);
        //    }
        //    return ret.ToString();
        //}
        public static string DetermineCompName(string UserHostName)
        {
            IPAddress myIP = IPAddress.Parse(UserHostName);
            IPHostEntry GetIPHost = Dns.GetHostEntry(myIP);
            List<string> compName = GetIPHost.HostName.ToString().Split('.').ToList();
            return compName.First();
        }
    }
}